<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ShareUrl;
use App\Models\Entrance;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Grid\Column;

class ShareUrlController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(ShareUrl::with('entrance'), function (Grid $grid) {
//            $grid->title('广告链接列表');
//            $grid->description('广告链接列表');
            $grid->column('id')->sortable();
            $grid->column('title','分享标题');
//            $grid->column('share_url','广告链接')->link();
            $grid->column('share_url2','广告链接')->display(function (){
               return  $this->entrance->entrance_web.'?r='.$this->resource_no;
            })->link();
            $grid->column('url_img','分享图片')->image(env('APP_URL').'uploads',100,100);
            $grid->column('remake','备注');
            $grid->column('status','访问状态')->bool(['1' => true, '2' => false]);
            $grid->column('entrance.entrance_name','入口模板');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ShareUrl(), function (Show $show) {
            $show->field('id');
            $show->field('title','分享标题');
            $show->field('share_url','分享链接');
            $show->field('url_img','分享图片')->image();
            $show->field('remake','备注');
            $show->field('status','访问状态')->bool(['1' => true, '2' => false]);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(ShareUrl::class, function (Form $form) {
            $form->display('id');

            $form->text('title','分享标题')->required();
            $form->text('share_url','分享链接')->required();
            $form->image('url_img','分享图片')->uniqueName()->required();
            $form->text('remarks','备注');
            $form->radio('status','访问状态')->options(['1' => '正常访问', '2' =>'停止访问'])->default(1);
            $resourceNo = md5( time().uniqid().rand(0,999));
            $options = Entrance::get()->toArray();
            $options = array_column($options,'entrance_name','id');
            $form->select('entrance_id','入口模板')->options($options)->required();
            $form->text('resource_no','系统资源标识')->value($resourceNo)->readOnly();
        });
    }
}
